/*
 * Copyright (c) 2020 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "camera_ability_slice.h"
#include <algorithm>
#include <meta_data.h>
#include <window/window.h>
#include "ability_env.h"

#include "ability_manager.h"
#include "gfx_utils/color.h"
#include "securec.h"
#include "ui_config.h"

namespace OHOS {
REGISTER_AS(CameraAbilitySlice)

CameraAbilitySlice::~CameraAbilitySlice()
{
    if (cam_manager != nullptr) {
        delete cam_manager;
    }
    if (backBttn != nullptr) {
        delete backBttn;
    }
    if (backIcon != nullptr) {
        delete backIcon;
    }
    if (buttonListener_ != nullptr) {
        delete buttonListener_;
    }
    if (surfaceView != nullptr) {
        delete surfaceView;
    }
}

void CameraAbilitySlice::SetHead(void)
{
    char iconPath[MAX_PATH_LENGTH] = {0};
    const char *pHead = GetSrcPath();

    if (sprintf_s(iconPath, sizeof(iconPath), "%s/%s", pHead, ICON_BACK0_PATH) < 0) {
        printf("ERROR!! get back icon path failed! \n");
        return;
    }

    backIcon = new UIImageView();
    backIcon->SetTouchable(true);
    backIcon->SetSrc((char *)iconPath);
    backIcon->SetPosition(BACK_LABEL_X, BACK_LABEL_Y, BACK_LABEL_W, BACK_LABEL_H);

    backBttn = new UIImageView();
    backBttn->SetTouchable(true);
    backBttn->SetPosition(BACK_AREA_X, BACK_AREA_Y, BACK_AREA_W, BACK_AREA_H);
    backBttn->SetStyle(STYLE_BACKGROUND_OPA, 0);
    auto backBttnonClick = [this](UIView &view, const Event &event) -> bool {

        AbilitySlice *nextSlice;
        Want wantData = { nullptr };
        nextSlice = AbilityLoader::GetInstance().GetAbilitySliceByName((char *)"HoodsAbilitySlice");
        if (nextSlice == nullptr) {
            TerminateAbility();

            return false;
        }

        Present(*nextSlice, wantData);

        return true;
    };

    buttonListener_ = new EventListener(backBttnonClick, nullptr);
    backBttn->SetOnClickListener(buttonListener_);
    backIcon->SetOnClickListener(buttonListener_);
}

void CameraAbilitySlice::OnStart(const Want &want)
{
    AbilitySlice::OnStart(want);
    printf("CameraAbilitySlice onstart \n");

    surfaceView = new UISurfaceView();
    surfaceView->SetPosition(CAMERA_START_X, CAMERA_START_Y, CAMERA_WIDTH, CAMERA_HEIGHT);
    surfaceView->GetSurface()->SetWidthAndHeight(IMAGE_WIDTH, IMAGE_HEIGHT);

    cam_manager = new SampleCameraManager();
    cam_manager->SampleCameraCreate();

    SetHead();

    RootView *rootView = RootView::GetWindowRootView();
    rootView->SetPosition(0, 0);
    rootView->Resize(CAMERA_WIDTH, CAMERA_HEIGHT);
    rootView->SetStyle(STYLE_BACKGROUND_COLOR, Color::ColorTo32(Color::Black()));

    rootView->Add(surfaceView);
    rootView->Add(backBttn);
    rootView->Add(backIcon);

    int timecnt = 0;
    while (1) {
        if (++timecnt > CAMERA_READY_TIMEOUT) {
            printf("wait camera timeout!! \n");
            break;
        }

        if (cam_manager->SampleCameraIsReady()) {
            break;
        }

        sleep(1);
    }
    SetUIContent(rootView);
}

void CameraAbilitySlice::OnInactive()
{
    printf("CameraAbilitySlice::OnInactive\n");
    AbilitySlice::OnInactive();
}

void CameraAbilitySlice::OnActive(const Want &want)
{
    printf("CameraAbilitySlice::OnActive\n");
    AbilitySlice::OnActive(want);

    if (cam_manager) {
        cam_manager->SampleCameraStart(surfaceView->GetSurface());
    }
}

void CameraAbilitySlice::OnBackground()
{
    printf("CameraAbilitySlice::OnBackground\n");
    AbilitySlice::OnBackground();
    if (cam_manager) {
        cam_manager->SampleCameraStop();
    }
}

void CameraAbilitySlice::OnStop()
{
    printf("CameraAbilitySlice::OnStop\n");
    AbilitySlice::OnStop();
}
}
